package cn.czh.org.controller;

import cn.czh.basic.exception.BusinessException;
import cn.czh.basic.util.AjaxResult;
import cn.czh.basic.util.ExcelUtils;
import cn.czh.basic.util.PageList;
import cn.czh.org.domain.Shop;
import cn.czh.org.domain.ShopAuditLog;
import cn.czh.org.domain.ShopDto;
import cn.czh.org.query.ShopQuery;
import cn.czh.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 店铺接口类
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口类",description = "店铺的CRUD操作")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     * 接口:查询所有
     * @return
     */
    //http://localhost:8080/shop - Get
    @GetMapping
    @ApiOperation(value = "查询所有接口" )
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口:查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/shop/35 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口")
    public Shop findOne(@PathVariable("id") Long id){

        return shopService.findOne(id);
    }

    /**
     * 接口：分页查询
     * @param shopQuery
     * @return
     */
    //http://localhost:8080/shop - Post 参数通过json对象传递
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    //@RequestBody - 从请求体中获取数据并赋值给shopQuery
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改接口")
    //@RequestBody - 从请求体中获取数据并赋值给shop
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            //如果没有获取id就是添加
            if (shop.getId() == null){
                shopService.add(shop);
            }else {
                //否则就是修改
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"操作失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObj("请重新操作");
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            return new AjaxResult(false,"系统异常,请稍后重试");
        }
    }

    /**
     * 接口：店铺审核成功
     * @param shopAuditLog
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统异常,请稍后重试");
        }
    }

    /**
     * 接口：店铺审核失败
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditReject(shopAuditLog);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统异常,请稍后重试");
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        // List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
        List<Shop> shops = shopService.findAll();
        ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"店铺1.0.xlsx",response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file")MultipartFile file){
    // String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass
        //0:表示标题行数 1:表示表头的行数
        List<Shop> shops = ExcelUtils.importExcel(file,0,1,Shop.class);
        //后台打印 也可以根据需求批量添加到数据库
        shops.forEach(System.out::println);
    }


    @GetMapping("/echarts")
    @ApiOperation(value = "查询所有接口" )
    public List<ShopDto> echarts(){
        return shopService.echarts();
    }


    @GetMapping("/active/{id}")
    public void active(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        Shop shop = shopService.findOne(id);
        shop.setState(3);
        shopService.update(shop);
        response.sendRedirect("http://localhost:8081/#/login");
    }


}
